<?php
/**
 * Created by PhpStorm.
 * User: xufei
 * Date: 2018/5/15
 * Time: 上午10:40
 */

class wxapi_ctl_aftersales extends wxapi_response
{
    public function __construct(&$app)
    {
        parent::__construct($app);
    }

    public function aftersalesList()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $size = isset($_GET['size']) ? $_GET['size'] : 10;
        $nPage =intval($page);
        $member = $this->get_current_member();
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        
        $filter = array();
        $filter["member_id"] = $member['member_id'];
        $obj_return_policy = kernel::service("aftersales.return_policy");
        $arr_settings = array();
        if (!isset($obj_return_policy) || !is_object($obj_return_policy)){
            self::api_error(app::get('aftersales')->_("售后服务应用不存在！"));
        }

        if (!$obj_return_policy->get_conf_data($arr_settings))        {
            self::api_error(app::get('aftersales')->_("售后服务信息没有取到！"));
        }

        $return_list = $obj_return_policy->get_return_product_list('*', $filter, $nPage);
        $totalCount = count($return_list);
        foreach($return_list['data'] as $key=>$val){
            $return_list['data'][$key]['product_data'] = unserialize($val['product_data']);
            $return_list['data'][$key]['comment'] = unserialize($val['comment']);
        }
 
        $oImage = app::get('image')->model('image');
        $oGoods = app::get('b2c')->model('goods');
        $oProducts = app::get('b2c')->model('products');
        $imageDefault = app::get('image')->getConf('image.set');
        foreach($return_list['data'] as $k => &$v) {
            foreach($v['product_data'] as $k2 => &$v2) {                
                $product_id = $v2['product_id'];
                $rs = $oProducts->dump($product_id, 'goods_id,spec_info');
                if($rs){
                    $goods_id = $rs['goods_id'];                    
                    $v2['spec_info'] = $rs['spec_info'];                    
                }
                
                $rs_goods = $oGoods->dump($goods_id, 'image_default_id');
                if($rs_goods['image_default_id']){
                    $v2['thumbnail_pic'] = base_storager::image_path($rs_goods['image_default_id']);
                }                
            }
        }

        $order_list = array();
        foreach ($return_list['data'] as $order){
            $itemList = [];
            foreach ($order['product_data'] as $key=>$goods){
                $itemList[$key] = array(
                    'auditStatus' => 1,
                    'evaluateStatus' => 0, //评价状态
                    'goodsId' => $goods['product_id'],
                    'goodsSkuId' => $goods['item_id'],
                    'goodsName' => $goods['name'],
                    'orderNo' => $goods['order_id'],
                    'id' => $goods['item_id'],
                    'num' => $goods['num'],
                    'goodsSkuVals' => (string)$goods['spec_info'],
                    'goodsLogo' => $goods['thumbnail_pic'],
                    'goodsThumLogo' => $goods['thumbnail_pic'],
                    'price' => number_format($goods['price'], 2),
                    'refundStatus' => 0, //0:正常,1:待审核,2:退款中,3:退款完成
                );
            }
            
            $btnStatus = 0;
            if(in_array($order['status'], array(3)) && !$order['customer_logistics']){
                $btnStatus = 1;
            }
            
            $status_def = array(
                '1' => ('未操作'),
                '2' => ('审核中'),
                '3' => ('接受申请'),
                '4' => ('完成'),
                '5' => ('拒绝'),
                '6' => ('已收货'),
                '7' => ('已质检'),
                '8' => ('补差价'),
                '9' => ('已拒绝退款'),
            );
            
            //物流处理 
            $order_list[] = array(
                'return_id' => $order['return_id'],
                'statusName' => $status_def[$order['status']],
                'auditStatus' => 0,
                'businessAccount' => '',    //店铺
                'businessId' => '',         //店铺id
                'businessName' => '',       //店铺id
                'createTime' => date('Y-m-d H:i:s',$order['add_time']),
                'deliverStatus' => $order['ship_status'], //发货状态：0未发货，1已发货，2部分发货，3部分退货，4已退货
                'freight' => $order['shipping']['cost_shipping'], //运费
                'goodsPrices' => number_format($order['total_amount'], 2), //商品总额
                'id' => $order['order_id'],
                'logisticsCode' => '', //物流公司编码
                'logisticsNo' => '', //物流单号
                'orderItemList' => $itemList,
                'orderNo' => $order['order_id'],
                "payStatus" => $order['pay_status'], //是否支付(0 未支付 1 已支付(或拼团成功) 2 处理中 3 部分付款 4 部分退款 5 全额退款) ,6付款成功，拼团没完成,7.拼团退款中,8拼团.等待退款
                "payTradeNo" => '', //支付单号
                "status" => $order['status'], //active:活动, dead:死单, finish:完成, pending:暂停
                "totalPrice" => $order['cur_amount'], //应付总额
                "btnStatus" => $btnStatus, //按钮状态
            );
        }
        
        $return = array(
            'result' => '0',
            'reason' => '',
            'code' => 0,
            'page_total' => ceil(count($order_list)/$size),
            'page_size' => $size,
            'list' => $order_list,
            'totalCount' => count($order_list),
            'pageNum' => $page,
        );

        return self::return_json($return);
    }
    /**
     * 得到订单列表详细
     * @param array 订单详细信息
     * @param string tpl
     * @return null
     */
    protected function get_order_details(&$aData,$tml='member_orders')
    {
        if (isset($aData['data']) && $aData['data'])
        {
            $objMath = kernel::single('ectools_math');
            // 所有的goods type 处理的服务的初始化.
            $arr_service_goods_type_obj = array();
            $arr_service_goods_type = kernel::servicelist('order_goodstype_operation');
            foreach ($arr_service_goods_type as $obj_service_goods_type)
            {
                $goods_types = $obj_service_goods_type->get_goods_type();
                $arr_service_goods_type_obj[$goods_types] = $obj_service_goods_type;
            }

            foreach ($aData['data'] as &$arr_data_item)
            {
                $this->get_order_detail_item($arr_data_item,$tml);
            }
        }
    }
    /**
     * 得到订单列表详细
     * @param array 订单详细信息
     * @param string 模版名称
     * @return null
     */
    protected function get_order_detail_item(&$arr_data_item,$tpl='member_order_detail')
    {
        if (isset($arr_data_item) && $arr_data_item)
        {
            $objMath = kernel::single('ectools_math');
            // 所有的goods type 处理的服务的初始化.
            $arr_service_goods_type_obj = array();
            $arr_service_goods_type = kernel::servicelist('order_goodstype_operation');
            foreach ($arr_service_goods_type as $obj_service_goods_type)
            {
                $goods_types = $obj_service_goods_type->get_goods_type();
                $arr_service_goods_type_obj[$goods_types] = $obj_service_goods_type;
            }


            $arr_data_item['goods_items'] = array();
            $obj_specification = app::get('b2c')->model('specification');
            $obj_spec_values = app::get('b2c')->model('spec_values');
            $obj_goods = app::get('b2c')->model('goods');
            $oImage = app::get('image')->model('image');
            if (isset($arr_data_item['order_objects']) && $arr_data_item['order_objects'])
            {
                foreach ($arr_data_item['order_objects'] as $k=>$arr_objects)
                {
                    $index = 0;
                    $index_adj = 0;
                    $index_gift = 0;
                    $image_set = app::get('image')->getConf('image.set');
                    if ($arr_objects['obj_type'] == 'goods')
                    {
                        foreach ($arr_objects['order_items'] as $arr_items)
                        {
                            if (!$arr_items['products'])
                            {
                                $o = $this->app->model('order_items');
                                $tmp = $o->getList('*', array('item_id'=>$arr_items['item_id']));
                                $arr_items['products']['product_id'] = $tmp[0]['product_id'];
                            }

                            if ($arr_items['item_type'] == 'product')
                            {
                                if ($arr_data_item['goods_items'][$k]['product'])
                                    $arr_data_item['goods_items'][$k]['product']['quantity'] = $objMath->number_plus(array($arr_items['quantity'], $arr_data_item['goods_items'][$k]['product']['quantity']));
                                else
                                    $arr_data_item['goods_items'][$k]['product']['quantity'] = $arr_items['quantity'];

                                $arr_data_item['goods_items'][$k]['product'] = $arr_items;
                                $arr_data_item['goods_items'][$k]['product']['name'] = $arr_items['name'];
                                $arr_data_item['goods_items'][$k]['product']['goods_id'] = $arr_items['goods_id'];
                                $arr_data_item['goods_items'][$k]['product']['price'] = $arr_items['price'];
                                $arr_data_item['goods_items'][$k]['product']['score'] = intval($arr_items['score']*$arr_data_item['goods_items'][$k]['product']['quantity']);
                                $arr_data_item['goods_items'][$k]['product']['amount'] = $arr_items['amount'];
                                $arr_goods_list = $obj_goods->getList('image_default_id,spec_desc', array('goods_id' => $arr_items['goods_id']));

                                $arr_goods = $arr_goods_list[0];
                                // 获取货品关联第一张图片
                                $select_spec_private_value_id = reset($arr_items['products']['spec_desc']['spec_private_value_id']);
                                $spec_desc_goods = reset($arr_goods['spec_desc']);
                                if($spec_desc_goods[$select_spec_private_value_id]['spec_goods_images']){
                                    list($default_product_image) = explode(',', $spec_desc_goods[$select_spec_private_value_id]['spec_goods_images']);
                                    $arr_goods['image_default_id'] = $default_product_image;
                                }else{
                                    if( !$arr_goods['image_default_id'] && !$oImage->getList("image_id",array('image_id'=>$arr_goods['image_default_id']))){
                                        $arr_goods['image_default_id'] = $image_set['S']['default_image'];
                                    }
                                }

                                $arr_data_item['goods_items'][$k]['product']['thumbnail_pic'] = $arr_goods['image_default_id'];
                                if ($arr_items['addon'])
                                {
                                    $arrAddon = $arr_addon = unserialize($arr_items['addon']);
                                    if ($arr_addon['product_attr'])
                                        unset($arr_addon['product_attr']);
                                    $arr_data_item['goods_items'][$k]['product']['minfo'] = $arr_addon;
                                }else{
                                    unset($arrAddon,$arr_addon);
                                }
                                if ($arrAddon['product_attr'])
                                {
                                    foreach ($arrAddon['product_attr'] as $arr_product_attr)
                                    {
                                        $arr_data_item['goods_items'][$k]['product']['attr'] .= $arr_product_attr['label'] . $arr_product_attr['value'];
                                    }
                                }

                                if (isset($arr_data_item['goods_items'][$k]['product']['attr']) && $arr_data_item['goods_items'][$k]['product']['attr'])
                                {
                                    if (strpos($arr_data_item['goods_items'][$k]['product']['attr']) !== false)
                                    {
                                        $arr_data_item['goods_items'][$k]['product']['attr'] = substr($arr_data_item['goods_items'][$k]['product']['attr'], 0, strrpos($arr_data_item['goods_items'][$k]['product']['attr']));
                                    }
                                }
                            }
                            elseif ($arr_items['item_type'] == 'adjunct')
                            {
                                $str_service_goods_type_obj = $arr_service_goods_type_obj[$arr_items['item_type']];
                                $str_service_goods_type_obj->get_order_object(array('goods_id' => $arr_items['goods_id'], 'product_id'=>$arr_items['products']['product_id']), $arrGoods,$tpl);


                                if ($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj])
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['quantity'] = $objMath->number_plus(array($arr_items['quantity'], $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['quantity']));
                                else
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['quantity'] = $arr_items['quantity'];

                                if (!$arrGoods['image_default_id'])
                                {
                                    $arrGoods['image_default_id'] = $image_set['S']['default_image'];
                                }
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj] = $arr_items;
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['name'] = $arr_items['name'];
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['score'] = intval($arr_items['score']*$arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['quantity']);
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['goods_id'] = $arr_items['goods_id'];
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['price'] = $arr_items['price'];
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['thumbnail_pic'] = $arrGoods['image_default_id'];
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['link_url'] = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_product','act'=>'index','arg0'=>$arr_items['products']['product_id']));
                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['amount'] = $arr_items['amount'];

                                if ($arr_items['addon'])
                                {
                                    $arr_addon = unserialize($arr_items['addon']);

                                    if ($arr_addon['product_attr'])
                                    {
                                        foreach ($arr_addon['product_attr'] as $arr_product_attr)
                                        {
                                            $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr'] .= $arr_product_attr['label'] . $this->app->_(":") . $arr_product_attr['value'] . " ";
                                        }
                                    }
                                }

                                if (isset($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr']) && $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr'])
                                {
                                    if (strpos($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr'], " ") !== false)
                                    {
                                        $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr'] = substr($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr'], 0, strrpos($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_adj]['attr'], " "));
                                    }
                                }

                                $index_adj++;
                            }
                            else
                            {
                                // product gift.
                                if ($arr_service_goods_type_obj[$arr_objects['obj_type']])
                                {
                                    $str_service_goods_type_obj = $arr_service_goods_type_obj[$arr_items['item_type']];
                                    $str_service_goods_type_obj->get_order_object(array('goods_id' => $arr_items['goods_id'], 'product_id'=>$arr_items['products']['product_id']), $arrGoods,$tpl);

                                    if ($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift])
                                        $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['quantity'] = $objMath->number_plus(array($arr_items['quantity'], $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['quantity']));
                                    else
                                        $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['quantity'] = $arr_items['quantity'];

                                    if (!$arrGoods['image_default_id'])
                                    {
                                        $arrGoods['image_default_id'] = $image_set['S']['default_image'];
                                    }
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift] = $arr_items;
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['name'] = $arr_items['name'];
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['goods_id'] = $arr_items['goods_id'];
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['price'] = $arr_items['price'];
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['thumbnail_pic'] = $arrGoods['image_default_id'];
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['score'] = intval($arr_items['score']*$arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['quantity']);
                                    $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['amount'] = $arr_items['amount'];

                                    if ($arr_items['addon'])
                                    {
                                        $arr_addon = unserialize($arr_items['addon']);

                                        if ($arr_addon['product_attr'])
                                        {
                                            foreach ($arr_addon['product_attr'] as $arr_product_attr)
                                            {
                                                $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr'] .= $arr_product_attr['label'] . ":" . $arr_product_attr['value'] . " ";
                                            }
                                        }
                                    }

                                    if (isset($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr']) && $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr'])
                                    {
                                        if (strpos($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr'], " ") !== false)
                                        {
                                            $arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr'] = substr($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr'], 0, strrpos($arr_data_item['goods_items'][$k][$arr_items['item_type'].'_items'][$index_gift]['attr'], " "));
                                        }
                                    }
                                }
                                $index_gift++;
                            }
                        }
                    }
                    else
                    {
                        if ($arr_objects['obj_type'] == 'gift')
                        {
                            if ($arr_service_goods_type_obj[$arr_objects['obj_type']])
                            {
                                foreach ($arr_objects['order_items'] as $arr_items)
                                {
                                    if (!$arr_items['products'])
                                    {
                                        $o = $this->app->model('order_items');
                                        $tmp = $o->getList('*', array('item_id'=>$arr_items['item_id']));
                                        $arr_items['products']['product_id'] = $tmp[0]['product_id'];
                                    }

                                    $str_service_goods_type_obj = $arr_service_goods_type_obj[$arr_objects['obj_type']];
                                    $str_service_goods_type_obj->get_order_object(array('goods_id' => $arr_items['goods_id'], 'product_id'=>$arr_items['products']['product_id']), $arrGoods,$tpl);

                                    if (!isset($arr_items['products']['product_id']) || !$arr_items['products']['product_id'])
                                        $arr_items['products']['product_id'] = $arr_items['goods_id'];

                                    if ($arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']])
                                        $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['quantity'] = $objMath->number_plus(array($arr_items['quantity'], $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['quantity']));
                                    else
                                        $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['quantity'] = $arr_items['quantity'];

                                    if (!$arrGoods['image_default_id'])
                                    {
                                        $arrGoods['image_default_id'] = $image_set['S']['default_image'];
                                    }

                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['name'] = $arr_items['name'];
                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['goods_id'] = $arr_items['goods_id'];
                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['price'] = $arr_items['price'];
                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['thumbnail_pic'] = $arrGoods['image_default_id'];
                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['point'] = intval($arr_items['score']*$arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['quantity']);
                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['nums'] = $arr_items['quantity'];
                                    
                                    $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['amount'] = $arr_items['amount'];

                                    if ($arr_items['addon'])
                                    {
                                        $arr_addon = unserialize($arr_items['addon']);

                                        if ($arr_addon['product_attr'])
                                        {
                                            foreach ($arr_addon['product_attr'] as $arr_product_attr)
                                            {
                                                $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr'] .= $arr_product_attr['label'] . ":" . $arr_product_attr['value'] . " ";
                                            }
                                        }
                                    }

                                    if (isset($arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr']) && $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr'])
                                    {
                                        if (strpos($arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr'], " ") !== false)
                                        {
                                            $arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr'] = substr($arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr'], 0, strrpos($arr_data_item[$arr_items['item_type'].'_items'][$arr_items['products']['product_id']]['attr'], " "));
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if ($arr_service_goods_type_obj[$arr_objects['obj_type']])
                            {

                                $str_service_goods_type_obj = $arr_service_goods_type_obj[$arr_objects['obj_type']];
                                $arr_data_item['extends_items'][] = $str_service_goods_type_obj->get_order_object($arr_objects, $arr_Goods,$tpl);
                            }
                        }
                    }
                }
            }

        }
    }

    /**
     * 保存售后申请
     */
    public function saveAftersale()
    {
        $member = $this->get_current_member();
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        
        $obj_return_policy = kernel::service("aftersales.return_policy");
        $arr_settings = array();
        if (!isset($obj_return_policy) || !is_object($obj_return_policy)) {
            $return = [
                'result' => '售后服务应用不存在',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        if (!$obj_return_policy->get_conf_data($arr_settings)) {
            $return = [
                'result' => '售后服务信息没有取到',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        $goods_items = $_GET['goods_items'];
        $order_id = $_GET['order_id'];
        if ( ! $goods_items) {
            $return = [
                'result' => '您没有选择商品，请先选择商品！',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        if (!$_GET['title']) {
            $return = [
                'result' => '请填写退货理由',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        $upload_file = "";
        if ( $_FILES['file']['size'] > 314572800 ){
            $return = [
                'result' => '上传文件不能超过30M！',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        if ( $_FILES['file']['name'] != "" ){
            $type=array("png","jpg","gif","jpeg","rar","zip");

            if(!in_array(strtolower($this->fileext($_FILES['file']['name'])), $type))
            {
                $text = implode(",", $type);
                $return = [
                    'result' => '您只能上传以下类型文件',
                    'code' => 5001,
                ];
                $this->end(false, '', '',false,'');
                return self::return_json($return);
            }

            $mdl_img = app::get('image')->model('image');
            $image_name = $_FILES['file']['name'];
            $image_id = $mdl_img->store($_FILES['file']['tmp_name'],null,null,$image_name);
            $mdl_img->rebuild($image_id,array('L','M','S'));

            if (isset($_REQUEST['type'])) {
                $type = $_REQUEST['type'];
            } else {
                $type = 's';
            }
            $image_src = base_storager::image_path($image_id, $type);
        }

        if( ! $_GET['agree']){
            $return = [
                'result' => '请先查看售后服务须知并且同意',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        //$obj_filter = kernel::single('b2c_site_filter');
        //$_GET = $obj_filter->check_input($_GET);

        $product_data = array();
        $order_products_quantity = $obj_return_policy->order_products_quantity($order_id);
        if( $goods_items ){
            $goodsList = json_decode(trim($goods_items),true);
            
            foreach ($goodsList as $val){
                $item = array();
                $item['bn']         = $val['product_bn'];
                $item['name']       = $val['product_name'];
                $item['num']        = $val['product_nums'] ? $val['product_nums'] : 1;
                $item['price']      = $val['product_price'];
                $item['product_id'] = $val['product_id'];
                $item['refund_amount'] = 0;
                if($order_products_quantity[$val['product_id']] < $item['num']){
                    continue;
                    //self::api_error('售后数量错误');
                } else {
                    $order_products_quantity[$val['product_id']] = 0;
                }
                $product_data[] = $item;
            }
        }

        $aData['order_id'] = $order_id;
        $aData['title'] = $_GET['title'];
        $aData['type'] = $_GET['type']==2 ? 2 : 1;
        $aData['add_time'] = time();
        $aData['image_file'] = $image_id;
        $aData['member_id'] = $member['member_id'];
        $aData['product_data'] = serialize($product_data);
        $aData['content'] = $_GET['content'];
        $aData['status'] = 2;
        $msg = "";
        $obj_aftersales = kernel::service("api.aftersales.request");
        if ($obj_aftersales && $obj_aftersales->generate($aData, $msg))
        {
            $obj_rpc_request_service = kernel::service('b2c.rpc.send.request');
            if ($obj_rpc_request_service && method_exists($obj_rpc_request_service, 'rpc_caller_request'))
            {
                if ($obj_rpc_request_service instanceof b2c_api_rpc_request_interface)
                    $obj_rpc_request_service->rpc_caller_request($aData,'aftersales');
            }
            else
            {
                $obj_aftersales->rpc_caller_request($aData);
            }
            $return = [
                'result' => '提交成功',
                'data' => array(),
                'code' =>  0,
            ];
            return self::return_json($return);
        }
        else
        {
            $return = [
                'result' => '保存失败',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
    }

    public function update()
    {
        $member = $this->get_current_member();
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        
        //logi_compay,    ,,    ,, openId, return_id        
        $_POST["logistics_company_name"] = $_GET['logi_compay'];
        $_POST["logistics_no"] = $_GET['logi_no'];
        $_POST["logistics_mobile"] = $_GET['mobile'];
        $_POST["logistics_address"] = $_GET['address'];
        $return_id = floatval($_GET['return_id']);

        $mdl_aftersales_return_product = app::get('aftersales')->model('return_product');        
        $return_info = $mdl_aftersales_return_product->dump(array("return_id"=>$return_id));
        if (empty($return_info)){
            self::api_error("不存在此退换货数据");
        }

        //请求接口处理
        $api_data = array(
            "return_id" => $return_id,
            "order_id" => $_POST['order_id'],
            "logistics_company_name" => $_POST['logistics_company_name'],
            "logistics_no" => $_POST['logistics_no']
        );
        $obj_apiv = kernel::single('b2c_apiv_exchanges_request');
        $obj_apiv->rpc_caller_request($api_data, 'aftersalelogisticsupdate');
        
        //本地保存填写的信息 用于后台展示
        $arr_customer_logistics = array(
            "物流公司" => $_POST["logistics_company_name"],
            "物流单号" => $_POST["logistics_no"],
            "收货手机号" => $_POST["logistics_mobile"],
            "收货地址" => $_POST["logistics_address"],
        );
        $result = $mdl_aftersales_return_product->update(array("customer_logistics"=>json_encode($arr_customer_logistics)),array("return_id"=>$return_id));
        if($result){
            $res = array(
                'code' => 0,
                'msg' => "保存成功",
            );
            self::return_json($res);
        }else{
            self::api_error("保存失败");
        }
    }

    private function get_current_member()
    {
        return self::user_auth();
    }

}